package com.company.cloud.behavior.modules.mgr.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.behavior.modules.api.response.CommentModel;
import com.company.cloud.behavior.modules.app.controller.req.CommentQueryByObjParam;
import com.company.cloud.behavior.modules.base.entity.UserCommentInfo;
import com.company.cloud.behavior.modules.cache.utils.BehaviorConvertUtils;
import com.company.cloud.behavior.modules.mgr.controller.req.CommentQuery4PageParam;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.company.cloud.behavior.modules.base.service.UserCommentInfoService;
import com.company.cloud.common.auth.annotation.Authc;

import io.swagger.annotations.Api;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 评论管理
 */
@Authc
@RestController
@RequestMapping("/mgr/behavior/comment")
@Api(tags = "Mgr_Behavior_评论管理")
public class CommentMgrController {
    @Autowired
    private UserCommentInfoService userCommentInfoService;
    @Autowired
    private UserRemoteService userRemoteService;

    @PostMapping("/query")
    @ApiOperation(value = "查询评论", response = CommentModel.class)
    public BaseResult query(@RequestBody CommentQuery4PageParam params) {
        ValidatorUtils.validate(params);
        IPage<UserCommentInfo> page = userCommentInfoService.page(new Page<UserCommentInfo>(params.getPage(), params.getRows()),
                new QueryWrapper<UserCommentInfo>()
                        .eq(CheckUtils.hasInt(params.getId()), "id", params.getId())
                        .eq(CheckUtils.hasInt(params.getUserId()), "user_id", params.getUserId())
                        .eq(StrUtil.isNotEmpty(params.getCompanyNo()), "company_no", params.getCompanyNo())
                        .eq(StrUtil.isNotEmpty(params.getObjType()), "obj_type", params.getObjType())
                        .eq(StrUtil.isNotEmpty(params.getObjNo()), "obj_no", params.getObjNo())
                        .orderByDesc("comment_time")
        );
        if (page == null || CollectionUtils.isEmpty(page.getRecords())) {
            return BaseResult.ok();
        }

        Set<Integer> userIds = new HashSet<Integer>();
        page.getRecords().forEach(item -> {
            userIds.add(item.getUserId());
        });
        BaseClientResult<Map<Integer, UserSimple>> userResult = userRemoteService.getUserMapByIds(userIds);
        Map<Integer, UserSimple> userMap = BaseClientResult.getResultData(userResult, new HashMap<>());

        IPage<CommentModel> data = page.convert(entity -> {
            CommentModel model = BehaviorConvertUtils.getCommentModel(entity, userMap);
            return model;
        });

        return BaseResult.ok(data);
    }


    @GetMapping("/get")
    @ApiOperation(value = "获取评论", response = CommentModel.class)
    public BaseResult get(@RequestParam("id") Integer id) {
        if (id == null || id <= 0) {
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        UserCommentInfo entity = userCommentInfoService.getById(id);
        if (entity == null) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }

        Set<Integer> userIds = new HashSet<Integer>();
        userIds.add(entity.getUserId());
        BaseClientResult<Map<Integer, UserSimple>> userResult = userRemoteService.getUserMapByIds(userIds);
        Map<Integer, UserSimple> userMap = BaseClientResult.getResultData(userResult, new HashMap<>());
        CommentModel data = BehaviorConvertUtils.getCommentModel(entity, userMap);
        return BaseResult.ok(data);
    }

    @GetMapping("/del")
    @ApiOperation(value = "删除评论")
    @StaffLogc(title = "Mgr_Behavior_评论管理删除", action = "MMgr_Behavior_评论管理删除", actionAdo = "Mgr_Behavior_评论管理列表")
    public BaseResult del(@RequestParam("id") Integer id) {
        if (id == null || id <= 0) {
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        userCommentInfoService.remove(new QueryWrapper<UserCommentInfo>().like("tree_path", StrUtil.format(",{},", id.toString())));
        userCommentInfoService.removeById(id);
        return BaseResult.ok();
    }

}
